/**
 * Created by Mark_ren on 2017/5/31.
 * 苟利国家生死以，岂因祸福避趋之
 */
$(function () {
    var item = getQueryString("item");
    var sort = getQueryString("sort");
    var title = "";
    var sel = $.Params.create();
    sel.addParams("page", 0);
    sel.addParams("rows", rows);
    sel.addParams("item", item);
    sel.addParams("sort", sort);
    getData();

    //查询
    $("#searchBtn").on("click", function () {
        var searchVal = $("#searchVal").val();
        var type = $("#types").val();
        if (searchVal != "") {
            sel.addParams("title", searchVal);
        } else {
            sel.addParams("title", undefined);
        }
        if (type != "0") {
            sel.addParams("type", type);
        } else {
            sel.addParams("type", undefined);
        }


        getData();
    })

    //判断checkbox状态
    $("#selAll").on("click", function () {
        if (!$(this).attr("checked")) {
            $("input[type=checkbox]").attr("checked", "checked")
        } else {
            $("input[type=checkbox]").removeAttr("checked");
        }
    })

    //添加事件
    $('#addDataBtn').on("click", function () {
        window.location.href = "keyanjiayouzhanAdd.html?item=" + item + "&sort=" + sort;
    });

    //删除事件
    $('#delBtn').on("click", function () {
        var pararms = getCheckData();
        delData(pararms);
    });

    var typeSel = $.Params.create();
    typeSel.ajax(getIP() + "api/v2.0/admin/dictionarys/" + sort, function (data) {
        if (data.success) {
            var typeHtml = "<option value = 0>请选择分类</option>";
            $.each(data.result, function (i, val) {
                typeHtml += "<option value = '" + val.codeId + "'>" + val.codeDesc + "</option>";
            })
            $("#types").html(typeHtml);
        } else {
            alert(data.msg);
        }
    })

    //获取数据
    function getData() {
        getPageData(sel, getIP() + "api/v2.0/admin/articles/JW/", function (data) {
            if (data.success) {
                var html = "";
                $.each(data.result, function (i, val) {
                    html += '<tr role="row" class="odd">' +
                        "<td><input eid='" + val.id + "' onclick='getCheck(this)' type='checkbox' class='classOption'></td>" +
                        "<td><img style='height:100px;width:100px;' src=" + getIP() + "data/articleCover/" + val.titlePic + "></td>" +
                        '<td>' + val.title + '</td>' +
                        '<td>' + val.createTime + '</td>' +
                        "<td><button  uid='" + val.id + "' onclick='updateInfo(this)'type='button' class='btn btn-primary'>修改</button><button type='button' onClick = 'setTop(\"" + val.id + "\")' class='btn btn-primary'>置顶</button></td></tr>";
                });
                $("#listContainer").html(html);
            }
        })
    }

    //置顶
    setTop = function (eid) {
        var topSel = $.Params.create();
        topSel.addParams("id", eid);
        topSel.ajax(getAdminIP() + "articles/TOP/", function (data) {
            if (data.success) {
                getData();
            } else {
                alert(data.msg);
            }
        })
    }
    //删除数据
    function delData(ids) {
        if (!ids == "") {
            var del = $.Params.create();
            del.addParams("params", ids)
            del.ajax(getIP() + "api/v2.0/admin/articles/del/", function (data) {
                if (data.success) {
                    alert("删除成功！");
                    getData();
                }
            })
        } else {
            alert("请至少选择一项");
        }
    }

    //修改数据
    updateInfo = function (obj) {
        var uId = $(obj).attr("uid");
        window.location.href = "keyanjiayouzhanUpdate.html?uid=" + uId + "&item=" + item + "&sort=" + sort;
    }
});
